/**
 * 
 * Copyright (C) 2011 Cody Stoutenburg . All rights reserved.
 *
 *       This program is free software; you can redistribute it and/or
 *       modify it under the terms of the GNU Lesser General Public License
 *       as published by the Free Software Foundation; either version 2.1
 *       of the License, or (at your option) any later version.
 *
 *       This program is distributed in the hope that it will be useful,
 *       but WITHOUT ANY WARRANTY; without even the implied warranty of
 *       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *       GNU Lesser General Public License for more details.
 *
 *       You should have received a copy of the GNU Lesser General Public License
 *       along with this program; if not, write to the Free Software
 *       Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. 
 * 
 */
package ca.usherbrooke.behaviour.manual;

import java.util.List;
import java.util.Random;

import ca.usherbrooke.agent.RandomAgent;
import ca.usherbrooke.agent.action.Action;
import ca.usherbrooke.agent.controller.shared.IBehaviour;
import ca.usherbrooke.application.astar.action.AStarMoveAction;
import ca.usherbrooke.application.astar.location.CompleteLocation;
import ca.usherbrooke.view.slick.jade.worldView.shared.EntityViewDebug;

/**
 * @author Cody Stoutenburg
 * 
 */
public class RandomMoveBehaviour extends IBehaviour<RandomAgent> {
	private static final long serialVersionUID = 1L;

	private final Random random;

	private Long previousTime;

	/**
	 * @param agent
	 */
	public RandomMoveBehaviour(RandomAgent agent) {
		super(agent);

		random = new Random();

		previousTime = this.getAgent().getWorld().getRealTimeElapse();
	}

	@Override
	public void action() {
		List<CompleteLocation> allLocation = this.getAgent()
				.getCompleteLocation();

		Long time = this.getAgent().getWorld().getRealTimeElapse();

		// position change each 500 ms
		if (time - previousTime > 3000) {
			previousTime = time;
			CompleteLocation loc = null;
			do {
				Integer x = random.nextInt(allLocation.size());
				loc = allLocation.get(x);

				EntityViewDebug.INSTANCE.setTargetPosition(this.getAgent()
						.getEntity().getGuid(), loc.getPosition());

			} while (!Action.canReachTargetPosition(getAgent(),
					loc.getPosition()));

			List<AStarMoveAction> plan = Action.generatePlanToTargetPosition(
					this.getAgent(), loc.getPosition());
			this.getAgent().setPlan(plan);
		}
	}

	@Override
	public boolean done() {
		return false;
	}

}
